﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TSB.SharePointUrlShortener.DAL;
using TypeMock.ArrangeActAssert;

namespace TSB.SharePointUrlShortener.UnitTests
{
    [TestClass]
    public class UrlShortenerRepositoryTest
    {
        [TestMethod]
        public void GetRedirectFromShortcode_ShortCodeNull()
        {
            var urlShortenerRepository = new UrlShortenerRepository();

            var redirectUrl = urlShortenerRepository.GetRedirectFromShortcode(null);

            Assert.AreEqual(string.Empty, redirectUrl);
        }

        [TestMethod]
        public void GetRedirectFromShortcode_ShortCodeStringEmpty()
        {
            var urlShortenerRepository = new UrlShortenerRepository();

            var redirectUrl = urlShortenerRepository.GetRedirectFromShortcode(string.Empty);

            Assert.AreEqual(string.Empty, redirectUrl);
        }

        [TestMethod]
        public void GetRedirectFromShortcode_NoList()
        {
            Isolate.WhenCalled(() => SPContext.Current.Site.RootWeb.Lists.TryGetList(Constants.LIST_URLSHORTENER)).WillReturn(null);

            var urlShortenerRepository = new UrlShortenerRepository();

            var redirectUrl = urlShortenerRepository.GetRedirectFromShortcode("shortcode");

            Assert.AreEqual(string.Empty, redirectUrl);
        }

        [TestMethod]
        public void CheckShortcodeExists_EmptyShortcode()
        {
            var urlShortenerRepository = new UrlShortenerRepository();
            var temp = string.Empty;
            var response = urlShortenerRepository.CheckShortcodeExists(string.Empty, out temp);

            Assert.AreEqual(response, false);
            Assert.AreEqual(temp, String.Empty);
        }

        [TestMethod]
        public void CheckShortcodeExists_NullShortcode()
        {
            var urlShortenerRepository = new UrlShortenerRepository();
            var temp = string.Empty;
            var response = urlShortenerRepository.CheckShortcodeExists(null, out temp);

            Assert.AreEqual(response, false);
            Assert.AreEqual(temp, String.Empty);
        }

        [TestMethod]
        public void CheckShortcodeExists_NoList()
        {
            Isolate.WhenCalled(() => SPContext.Current.Site.RootWeb.Lists.TryGetList(Constants.LIST_URLSHORTENER)).WillReturn(null);

            var urlShortenerRepository = new UrlShortenerRepository();
            var temp = string.Empty;
            var response = urlShortenerRepository.CheckShortcodeExists("testshortcode", out temp);

            Assert.AreEqual(response, false);
            Assert.AreEqual(temp, String.Empty);
        }
    }
}
